package com.flyduck.system.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyduck.system.entity.SysDeptEntity;
import com.flyduck.system.enums.StatusEnum;
import com.flyduck.system.mapper.SysDeptMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: flyduck-platform2
 * @description:
 * @author: flyduck
 * @create: 2021-04-23 14:55
 **/
@Repository
public class SysDeptDao extends ServiceImpl<SysDeptMapper, SysDeptEntity> {

    /**
     * 获取未删除且启用的部门列表
     * @return
     */
    public List<SysDeptEntity> findAllDept() {
        QueryWrapper<SysDeptEntity> wrapper = getDeptQueryWrapper();
        return this.baseMapper.selectList(wrapper);
    }

    private QueryWrapper<SysDeptEntity> getDeptQueryWrapper() {
        QueryWrapper<SysDeptEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusEnum.ENABLE.getValue());
        return wrapper;
    }

    /**
     * 根据部门id查询部门
     * @param deptId
     * @return
     */
    public SysDeptEntity findDeptById(Long deptId) {
        return this.baseMapper.selectById(deptId);
    }

    /**
     * 根据父部门id查询部门集合
     * @param parentId
     * @return
     */
    public List<SysDeptEntity> findByParentId(Long parentId){
        QueryWrapper<SysDeptEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        return this.baseMapper.selectList(wrapper);
    }
}
